$(function(){
	var employeeDatagrid = $("#employee_datagrid");
	var employeeDialog = $("#employee_dialog");
	var employeeDialogForm = $("#employee_dialog_form");
	var employeeDatagridEditBtn = $("#employee_datagrid_bt a");
	employeeDatagrid.datagrid({
		url:"/employee_list",
		fit:true,
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		columns:[[
			{title:"账号",field:"name",width:1},
			{title:"真实姓名",field:"realname",width:1},
			{title:"密码",field:"password",width:1},
			{title:"联系方式",field:"tel",width:1},
			{title:"邮箱",field:"email",width:1},
			{title:"部门",field:"dept",width:1,formatter:function(value,row,index){
				if(value){
					return value.name;
				}else{
					return value;
				}
			}},
			{title:"入职时间",field:"inputtime",width:1},
			{title:"状态",field:"state",width:1,formatter:stateFormatter},
		]],
		fitColumns:true,
		toolbar:"#employee_datagrid_bt",
		pageList:[1,5,10,20],
		onClickRow:function(rowIndex, rowData){
			if(rowData.state){
				employeeDatagridEditBtn.eq(1).linkbutton("enable");
				employeeDatagridEditBtn.eq(2).linkbutton("enable");
			}else{
				employeeDatagridEditBtn.eq(1).linkbutton("disable");
				employeeDatagridEditBtn.eq(2).linkbutton("disable");
			}
		}
	});
	employeeDialog.dialog({
		width:250,
		height:250,
		buttons:"#employee_dialog_bb",
		modal:true,
		closed:true
	});
	$("#employee_department_select").combobox({
		url:"/department_list",
		valueField:'id',    
	    textField:'name' 
	});
	var employeeRoleSelect = $("#employee_role_select");
	employeeRoleSelect.combobox({
		url:"/role_selectAll",
		valueField:'id',
		textField:'name',
		multiple:true
	});
	var cmdObj={
			add:function(){
				employeeDialog.dialog("open").dialog("setTitle","新增");
				employeeDialogForm.form("clear");
			},
			edit:function(){
				var rowData = employeeDatagrid.datagrid("getSelected");
				if(rowData){
					employeeDialog.dialog("open").dialog("setTitle","修改");
					employeeDialogForm.form("clear");
					if(rowData.dept){
						rowData["dept.id"]=rowData.dept.id;
					}
					employeeDialogForm.form("load",rowData);
					var html = $.ajax({
						  url: "/role_selectByEid?id="+rowData.id,
						  async: false
						 }).responseText;
					html = $.parseJSON(html);
					console.log(html);
					employeeRoleSelect.combobox("setValues",html);
				}else{
					$.messager.alert("温馨提示","请选择一行要修改的数据","info");
				}
			},
			del:function(){
				var rowData = employeeDatagrid.datagrid("getSelected");
				if(rowData){
					$.messager.confirm("确认对话框","您确定要离职该名员吗？",function(r){
						if(r){
							$.get("/employee_delete?id="+rowData.id,function(data){
								if(data.success){
									$.messager.alert("温馨提示",data.msg,"info",function(){
										employeeDatagrid.datagrid("load");
									});
								}else{
									$.messager.alert("温馨提示",data.msg,"warning");
								}
							},"json")
						}
					});
				}else{
					$.messager.alert("温馨提示","请选择一行要离职的员工数据","info");
				}
				
			},
			refresh:function (){
				employeeDatagrid.datagrid("reload");
			},
			cancel:function (){
				employeeDialog.dialog("close");
			},
			save:function (){
				var id = $("input[name='id']").val();
				var url;
				if(id){
					url="/employee_update";
				}else{
					url="/employee_save";
				}
				employeeDialogForm.form("submit",{
					url:url,
					onSubmit:function(param){
						var values = employeeRoleSelect.combobox("getValues");
						for (var i = 0; i < values.length; i++) {
							param["roles["+i+"].id"]=values[i];
						}
					},
				    success:function(data){    
				        data = $.parseJSON(data);
				        if(data.success){
				        	$.messager.alert("温馨提示",data.msg,"info",function(){
				        		employeeDialog.dialog("close");
				        		employeeDatagrid.datagrid("load");
				        	});
				        }else{
				        	$.messager.alert("温馨提示",data.msg,"warning");
				        }
				    } 
				});
			},
			searchContent:function (){
				var arr = $("#employee_search_form").serializeArray();
				var param = {};
				for(var i=0;i<arr.length;i++){
					param[arr[i].name]=arr[i].value;
				}
				employeeDatagrid.datagrid("load",param);
			}
	}
	$("a[data-cmd]").on("click",function(){
		var cmd = $(this).data("cmd");
		if(cmd){
			cmdObj[cmd]();
		}
	});
});
function stateFormatter(value,row,index){
	if(value){
		return "<font color='green'>在职</font>"
	}else{
		return "<font color='red'>离职</font>"
	}
}
